#include <sc95f.H>
#include <User/KEY/KEY.h>
#include <User/OLED/OLED.h>
#include <User/Moisture/Moisture.h>
#include <User/Light/Light.h>
#include <User/Max6675/Max6675.h>
#include <User/Communicate/Communicate.h>
#include <User/Clock/Clock.h>

#define BAUD 9600
#define MCU_FREQUENCY 32

#define OLD_MAIN 0
#define MAIN 1
#define KEY 2
#define ADCaa 3
#define MOISTURE 4

#define RUN_STATE MOISTURE

#if RUN_STATE == OLD_MAIN

// 主程序
void main()
{
    GPIO_Init();
    P30 = 0;
    PWM0_Init();
    PWM2_Init();
    Timer0_Init();
    KEY_Init();
    Input_Init();
    Uart0_Init(MCU_FREQUENCY, BAUD);
    OLED_Init();
    OLED_Init(); // 此处初始化两次，是因为初始化一次OLED会出问题
    OLED_clear();
    OLED_showchar(0,0,'0');
    while (1)
    {
        // 休息模式
        // 当时钟数到10s时IR置0，进入该模式
        if (Timer0_rest)
        {
            // 休息模式，关灯
            PWM0_LightAll(offlight);
            OLED_clear();
            // 休息模式，关闭 ESP8266 和 OLED 的电源
            GPIO_turnoff_ESP8266_OLED();
            Input_lock();
            while (Timer0_rest)
            {
                Input_restloop();
            }
        }

        // 密码输入流程
        if (Input_password_op && Timer0_work)
        {
            notouchTime = 0;
            OLED_showstring(0, 0, "password");
            while (Input_password_op && Timer0_work)
            {
                Input_passwordloop();
            }
        }

        // 手势输入流程
        if (Input_pattern_op && Timer0_work)
        {
            notouchTime = 0;
            OLED_patternInit();
            while (Input_pattern_op && Timer0_work)
            {
                Input_patternloop();
            }
        }

        // 管理员控制
        if (Input_admin_op && Timer0_work)
        {
            notouchTime = 0;
            OLED_clear();
            menu_op = main_menu;
            OLED_showstring(0, 0, " admin");
            OLED_showadminmenu(main_menu);
            while (Input_admin_op && Timer0_work)
            {
                Input_adminloop();
            }
            if (Input_pattern_op)
            {
                notouchTime = 0;
                while (notouchTime < 0x03)
                    KEY_Scan();
                PWM0_LightAll(lowlight);
            }
        }
    }
}

#endif

#if RUN_STATE == MAIN

int main()
{
    GPIO_Init();
    KEY_Init();
    OLED_Init();
    OLED_Init(); // 此处初始化两次，是因为初始化一次OLED会出问题
    PWM0_Init();
    while(1)
    {
        k00 = KEY_Scan();
        OLED_showbinary(0, k00);
        OLED_showbinary(1, i++);
    }
}

#endif

#if RUN_STATE == KEY

int main()
{
    uint8_t i = 0;
    uint8_t k00;
    KEY_Init();
    OLED_Init();
    OLED_Init(); // 此处初始化两次，是因为初始化一次OLED会出问题
    while(1)
    {
        k00 = KEY_Scan();
        OLED_showbinary(0, k00);
        OLED_showbinary(1, i++);
    }
}

#endif

#if RUN_STATE == ADCaa

int main()
{
    uint16_t ad;
    double adc_result = 0;
    uint8_t i=0;
    ADC_init(AIN0, PERIOD_32sys, VREFS_VDD);
    while(1)
    {
        i++;
        ad = ADC_getVoltageSync(ADC0);
        adc_result = (double)(ad)/0xFFF*3.3;
    }
}

#endif

#if RUN_STATE == MOISTURE

void main()
{
    uint8_t moist = 0, light = 0;
    uint8_t i=0;
    uint16_t s = 0;
    double temp = 0;
    uint8_t k = 0;
    P0CON |= BIT(2)|BIT(3);
    KEY_Init();
    Moisture_init();
    Light_init();
    Max6675_init();
    OLED_Init();
    Communicate_init(Computer_Uart);
    Clock_init();

    while(1)
    {
        i++;


        OLED_ShowNum(4, 14, i, 3);

        moist = Moisture_get();
        light = Light_get();

        k = KEY_Scan();

        if(!(i % 5))
        {
            temp = Max6675_getT();
        }

        OLED_ShowString(1, 1, "moist:");
        OLED_ShowNum(1, 7, moist, 3);
        OLED_ShowChar(1, 10, '%');
        OLED_ShowString(2, 1, "light:");
        OLED_ShowNum(2, 7, light, 3);
        OLED_ShowChar(2, 10, '%');
        OLED_ShowString(3, 1, "tempu:");
        OLED_ShowNum(3, 7, temp, 4);
        OLED_ShowChar(3, 11, 'C');
        OLED_ShowString(4, 1, "k:");
        OLED_ShowNum(4, 3, k, 1);
        OLED_ShowString(4, 5, "t:");
        OLED_ShowNum(4, 7, Clock_mills(), 5);
    }
}

#endif
